import axios from 'axios'
import { getAccessToken } from "@/utils/auth";

axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'

// 请求白名单，无须token的接口
const whiteList: string[] = ['/user/login/admin']

// 创建axios实例
const service = axios.create({
  // axios中请求配置有baseURL选项，表示请求URL公共部分
  baseURL: import.meta.env.VITE_APP_BASE_API,
  // 超时
  timeout: 10000
})

// request拦截器
service.interceptors.request.use(config => {
  let needToken = !whiteList.some((i) => {
    return i === config.url
  })

  if (needToken) config.headers['Authorization'] = 'Bearer ' + getAccessToken()
  return config
})

export default service
